package com.zp.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.util.Properties;

public class DruidUtil {

    private static DruidDataSource ds;

    static {
        try {
            InputStream is = DruidUtil.class.getResourceAsStream("/database.properties");
            Properties properties = new Properties();
            properties.load(is);

            // 测试是否多次执行
            // System.out.println("执行了一次"); 即便多次获取，仍仅仅读取一次
            ds = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static DataSource getDataSource() {
        if (ds != null) {
            return ds;
        }
        return null;
    }

}
